package com.diploma.interaction.controller;

import com.diploma.interaction.model.DmQxSwrysfTest;
import com.diploma.interaction.model.WxxcxDlzhxx;
import com.diploma.interaction.model.WxxcxWz;
import com.diploma.interaction.service.SwrysfService;
import com.diploma.interaction.service.WzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping(value = "/v1/article")
@Slf4j
public class ArticleController {

    @Autowired
    WzService wzService;

    @Autowired
    SwrysfService swrysfService;

    @RequestMapping(value = "/getStaffArticleList", method = RequestMethod.GET)
    String getStaffArticleList(HttpSession session, Model model) {
        WxxcxDlzhxx staff= (WxxcxDlzhxx) session.getAttribute("staff");
        if (staff!=null) {
            model.addAttribute("wz", new WxxcxWz());
            model.addAttribute("emps", wzService.selectByStaffId(staff.getSwryDm()));
            return "myArticle";
        } else {
            return "redirect:/v1/login/notVerify";
        }

    }

    @RequestMapping(value = "/sendArticle", method = RequestMethod.GET)
    String sendArticle(@RequestParam("id") int id, HttpSession session, Model model ) {
        WxxcxDlzhxx staff= (WxxcxDlzhxx) session.getAttribute("staff");
        if (staff!=null) {
            WxxcxWz wz=new WxxcxWz();
            wz.setZt((short) 1);
            wzService.updateByIdSelective(wz,id);
            model.addAttribute("emps", wzService.selectByStaffId(staff.getSwryDm()));
            return "myArticle";
        } else {
            return "redirect:/v1/login/notVerify";
        }

    }

    @RequestMapping(value = "/setArticle", method = RequestMethod.GET)
    String setArticle(@RequestParam("id") int id, HttpSession session, Model model ) {
        WxxcxDlzhxx staff= (WxxcxDlzhxx) session.getAttribute("staff");
        if (staff!=null) {
            WxxcxWz wz=wzService.selectById(id);
            model.addAttribute("wz", wz);
            session.setAttribute("setWzid",id);
            if(wz.getYdl()==null){
                session.setAttribute("setYdl",0);
            }else{
                session.setAttribute("setYdl",wz.getYdl());
            }

            log.info("set,wzid={}", wz.getWzid());
            return "setArticle";
        } else {
            return "redirect:/v1/login/notVerify";
        }

    }

    @RequestMapping("/createArticle")
    String login(Model model) {
        model.addAttribute("wz", new WxxcxWz());
        return "createArticle";
    }

    @RequestMapping(value = "/staffCreateArticle", method = RequestMethod.POST)
    String createArticle(WxxcxWz wz, Model model, HttpSession session) {
        WxxcxDlzhxx staff= (WxxcxDlzhxx) session.getAttribute("staff");
        if (staff!=null) {
            wz.setZt((short) 1);
            wz.setSwryDm(staff.getSwryDm());
            //获得税务局名称
            DmQxSwrysfTest swry=swrysfService.selectById(staff.getSwryDm());
            log.info("税收人员身份,swry={}", swry);
            String rysfmc= (String) swry.getRysfmc();
            String [] arr = rysfmc.split("\\s+");
            wz.setSwryXm(arr[0]);
            wz.setSwjgMc(arr[1]);

            wz.setFbsj(new Date());
            wzService.insertSelective(wz);

            model.addAttribute("emps", wzService.selectByStaffId(staff.getSwryDm()));
            return "myArticle";
        } else {
            return "redirect:/v1/login/notVerify";
        }

    }

    @RequestMapping(value = "/staffSaveArticle", method = RequestMethod.POST)
    String saveArticle(WxxcxWz wz, Model model, HttpSession session) {
        WxxcxDlzhxx staff= (WxxcxDlzhxx) session.getAttribute("staff");
        if (staff!=null) {
            wz.setZt((short) 0);
            log.info(String.valueOf(wz));
            wz.setSwryDm(staff.getSwryDm());
            //获得税务局名称
            DmQxSwrysfTest swry=swrysfService.selectById(staff.getSwryDm());
            log.info("税收人员身份,swry={}", swry);
            String rysfmc= (String) swry.getRysfmc();
            String [] arr = rysfmc.split("\\s+");
            wz.setSwryXm(arr[0]);
            wz.setSwjgMc(arr[1]);

            wz.setFbsj(new Date());
            wzService.insertSelective(wz);

            model.addAttribute("emps", wzService.selectByStaffId(staff.getSwryDm()));
            return "myArticle";
        } else {
            return "redirect:/v1/login/notVerify";
        }

    }
    @RequestMapping(value = "/staffSetCreateArticle", method = RequestMethod.POST)
    String setCreateArticle(WxxcxWz wz, Model model, HttpSession session) {
        WxxcxDlzhxx staff= (WxxcxDlzhxx) session.getAttribute("staff");
        if (staff!=null) {
            wz.setZt((short) 1);
            wz.setSwryDm(staff.getSwryDm());
            //获得税务局名称
            DmQxSwrysfTest swry=swrysfService.selectById(staff.getSwryDm());
            log.info("税收人员身份,swry={}", swry);
            String rysfmc= (String) swry.getRysfmc();
            String [] arr = rysfmc.split("\\s+");
            wz.setSwryXm(arr[0]);
            wz.setSwjgMc(arr[1]);

            wz.setFbsj(new Date());
            wz.setWzid(Short.valueOf(session.getAttribute("setWzid").toString()));
            wz.setYdl(Short.valueOf(session.getAttribute("setYdl").toString()));
            wzService.updateByIdSelective(wz,wz.getWzid());

            model.addAttribute("emps", wzService.selectByStaffId(staff.getSwryDm()));
            return "myArticle";
        } else {
            return "redirect:/v1/login/notVerify";
        }

    }

    @RequestMapping(value = "/staffSetSaveArticle", method = RequestMethod.POST)
    String setSaveArticle(WxxcxWz wz, Model model, HttpSession session) {
        WxxcxDlzhxx staff= (WxxcxDlzhxx) session.getAttribute("staff");
        if (staff!=null) {
            //wz.setZt((short) 0);
            log.info(String.valueOf(wz));
            wz.setSwryDm(staff.getSwryDm());
            //获得税务局名称
            DmQxSwrysfTest swry=swrysfService.selectById(staff.getSwryDm());
            log.info("税收人员身份,swry={}", swry);
            String rysfmc= (String) swry.getRysfmc();
            String [] arr = rysfmc.split("\\s+");
            wz.setSwryXm(arr[0]);
            wz.setSwjgMc(arr[1]);

            wz.setFbsj(new Date());

            wz.setWzid(Short.valueOf(session.getAttribute("setWzid").toString()) );
            wz.setYdl(Short.valueOf(session.getAttribute("setYdl").toString()));
            wzService.updateByIdSelective(wz,wz.getWzid());
            model.addAttribute("emps", wzService.selectByStaffId(staff.getSwryDm()));
            return "myArticle";
        } else {
            return "redirect:/v1/login/notVerify";
        }

    }

    @RequestMapping(value = "/deleteArticle", method = RequestMethod.GET)
    String deleteArticle(@RequestParam("id") int id,Model model, HttpSession session ) {
        WxxcxDlzhxx staff= (WxxcxDlzhxx) session.getAttribute("staff");
        if (staff!=null) {
            wzService.deleteById(id);
            model.addAttribute("emps", wzService.selectByStaffId(staff.getSwryDm()));
            return "myArticle";
        } else {
            return "redirect:/v1/login/notVerify";
        }

    }
}
